package com.example.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBOpenHelper extends SQLiteOpenHelper {
	// 库名称
	private static final String DB_NAME = "MCode3.db";
	// 表名称
	private static final String TBL_NAME = "MCodeTable3";

	// 存储登录名的表
	private static final String TBL_NAME2 = "MCodeTable4";
	// 创建表的名称
	private static final String CREATE_TBL = "create table MCodeTable3( _id integer not null primary key autoincrement,company nvarchar(40),account nvarchar(20),password  nvarchar(40),mark nvarchar(100))";
	private static final String CREATE_TBL2 = "create table MCodeTable4( _id integer not null primary key autoincrement,password nvarchar(40))";
	// SQLiteDatabase实例
	private SQLiteDatabase db;

	/*
	 * 
	 * 构造方法 (non-Javadoc)
	 * 
	 * @see
	 * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
	 * .SQLiteDatabase)
	 */
	public DBOpenHelper(Context c) {
		super(c, DB_NAME, null, 2);
	}

	/*
	 * 创建表 (non-Javadoc)
	 * 
	 * @see
	 * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
	 * .SQLiteDatabase)
	 */

	@Override
	public void onCreate(SQLiteDatabase db) {
		this.db = db;
		System.out.println(db.getPath());
		db.execSQL(CREATE_TBL);
		db.execSQL(CREATE_TBL2);

	}

	/*
	 * 
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
	 * .SQLiteDatabase, int, int)
	 */
	public void insert(ContentValues values) {
		// 获得SQLitedatabase实例
		SQLiteDatabase db = getWritableDatabase();
		// 插入
		db.insert(TBL_NAME, null, values);
		// 插入的是登录界面的密码
		// db.insert(CREATE_TBL2, null, values);
		// 关闭
		db.close();
	}

	public void insert2(ContentValues values) {
		// 获取数据库sqlitedatabase的实例
		SQLiteDatabase db = getWritableDatabase();
		// 插入
		db.insert(TBL_NAME2, null, values);

		db.close();
	}

	/*
	 * 查询方法 (non-Javadoc)
	 * 
	 * @see
	 * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
	 * .SQLiteDatabase, int, int)
	 */
	public Cursor query() {
		// 获得SQLiteDataBase实例
		SQLiteDatabase db = getWritableDatabase();
		// 查询获得cursor
		String[] str = { "_id", "company", "account", "password", "mark" };
		Cursor c = db.query(TBL_NAME, str, null, null, null, null, null);
		// System.out.println(c);// 输出c的值
		return c;

	}

	public String queryPassword() {// 查询登录密码
		// 获取数据库实例
		SQLiteDatabase db = getWritableDatabase();
		// 查询获取cursor
		String[] str = { "password" };
		Cursor cursor = db.query(TBL_NAME2, str, null, null, null, null, null);
		if (cursor.moveToNext())
			return cursor.getString(0);// 将游标指向的值转换为string类型并输出！
		return null;

	}

	/*
	 * 删除的方法 (non-Javadoc)
	 * 
	 * @see
	 * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
	 * .SQLiteDatabase, int, int)
	 */
	public void del(long temp) {
		if (db == null) {
			// 获得sqliteDatabase的实例
			db = getWritableDatabase();
		}

		// 执行删除
		db.delete(TBL_NAME, null, null);
	}

	/*
	 * 关闭数据库 (non-Javadoc)
	 * 
	 * @see
	 * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
	 * .SQLiteDatabase, int, int)
	 */
	public void close() {
		if (db != null) {
			db.close();
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}

}